package top.itaso.app.ui.titlebar;

import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import top.itaso.app.R;
import top.itaso.app.common.BaseDialogFragment;
import top.itaso.lib.common.ToastUtils;
import top.itaso.lib.widget.CommonDialog;
import top.itaso.lib.widget.tpv.TpvCommonItemView;
import top.itaso.lib.widget.tpv.TpvFileMenuBarView;
import top.itaso.lib.widget.tpv.TpvSettingsTitleBarView;
import top.itaso.lib.widget.tpv.TpvSettingsTitleBarView2AppsFilter;

/**
 * @Description:IWB 自定义控件展示
 * 1.TpvSettingsTitleBarView/TpvSettingsTitleBarView2AppsFilter
 * 2.TpvCommonItemView
 * 3.TpvFileMenuBarView
 * @Author: itsdf07
 * @CreateDate: 2022/8/19
 */
public class TitleBarFragment extends Fragment {
    private static final String TAG = "TitleBarFragment";

    private TitleBarViewModel mViewModel;

    private TpvSettingsTitleBarView titleBarViewOnlyTitle;
    private TpvSettingsTitleBarView titleBarViewWifi;
    private TpvSettingsTitleBarView titleBarView2RightBtn;
    private TpvSettingsTitleBarView2AppsFilter titleBarViewFilter;

    private TpvCommonItemView tpvLayoutItem1;
    private TpvCommonItemView tpvLayoutItem5Arrow;
    private TpvCommonItemView tpvLayoutItem5Switch;
    private TpvCommonItemView tpvLayoutItem5Custom;
    private TpvCommonItemView tpvLayoutItem21Arrow;
    private TpvCommonItemView tpvLayoutItem23Arrow;
    private TpvCommonItemView tpvLayoutItem23Switch;
    private TpvCommonItemView tpvLayoutItem31Arrow;
    private TpvCommonItemView tpvLayoutItem31Switch;
    private TpvCommonItemView tpvLayoutItem37Switch;
    private TpvCommonItemView tpvLayoutItem63Switch;

    private TpvFileMenuBarView tpvLayoutFileMenubar;

    public static TitleBarFragment newInstance() {
        return new TitleBarFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mViewModel = new ViewModelProvider(this).get(TitleBarViewModel.class);
        View rootView = inflater.inflate(R.layout.fragment_titlebar, container, false);
        initView(rootView);
        return rootView;
    }

    private void initView(View rootView) {
        titleBarViewOnlyTitle = rootView.findViewById(R.id.layout_titlebar_view_noback);
        titleBarViewOnlyTitle.setTitleContent("系统更新");
        titleBarViewOnlyTitle.hideBack();

        //------------------------------------------------------------------------------------------

        titleBarViewWifi = rootView.findViewById(R.id.layout_titlebar_view_wifi);
        titleBarViewWifi.setTitleContent("无线网络");
        ImageView searchStatus = titleBarViewWifi.createRightImage();
        Switch wifiSwitch = titleBarViewWifi.createRightSwitch();
        wifiSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                rotateView(searchStatus, isChecked);
            }
        });

        titleBarViewWifi.addTitleBarRight(searchStatus, wifiSwitch).setLayoutReadyOk();

        //------------------------------------------------------------------------------------------
        titleBarView2RightBtn = rootView.findViewById(R.id.layout_titlebar_view);
        titleBarView2RightBtn.setTitleContent("密码设置");
        titleBarView2RightBtn.setOnPrivateMarkCallback(new TpvSettingsTitleBarView.IOnPrivateMarkCallback() {
            @Override
            public void onPrivateMarkOpen() {
                CommonDialog dialog = new CommonDialog.Builder(getContext(), CommonDialog.EDialogDefined.whole)
                        .setTitle("暗门事件")
                        .setMessage("这是一个暗门事件")
                        .setPositiveClickListener("确定", new CommonDialog.IPositiveClickListener() {

                            @Override
                            public void onClick() {
                                Log.d(TAG, "onConirm: >>>>>>");
                            }
                        })
                        .setNegativeClickListener("", new CommonDialog.INegativeClickListener() {
                            @Override
                            public void onClick() {
                                Log.d(TAG, "onCancel: >>>>>>");
                            }
                        })
                        .build();
                dialog.show();
            }
        });

        TextView pwdSet = titleBarView2RightBtn.createTitleRightBtnTextView();
        pwdSet.setBackground(getResources().getDrawable(R.drawable.selector_bg_rounded_rectangle_large));
        pwdSet.setText("设置密码");
        pwdSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View rootView = LayoutInflater.from(getContext()).inflate(R.layout.layout_pwd_setting, null);
                rootView.setBackgroundColor(getContext().getResources().getColor(R.color.transparent));
                EditText etPwdInput = rootView.findViewById(R.id.et_input_password);
                CommonDialog dialog = new CommonDialog.Builder(getContext(), CommonDialog.EDialogDefined.whole)
                        .setTitle("设置密码")
                        .setContentView(rootView)
                        .setPositiveClickListener("保存", new CommonDialog.IPositiveClickListener() {

                            @Override
                            public void onClick() {
                                Log.d(TAG, "onConirm: >>>>>>" + etPwdInput.getText().toString());
                            }
                        })
                        .setNegativeClickListener("", new CommonDialog.INegativeClickListener() {
                            @Override
                            public void onClick() {
                                Log.d(TAG, "onCancel: >>>>>>");
                            }
                        })
                        .build();
                dialog.show();
            }
        });

        TextView pwdModify = titleBarView2RightBtn.createTitleRightBtnTextView();
        pwdModify.setBackground(getResources().getDrawable(R.drawable.selector_bg_rounded_rectangle_large));
        pwdModify.setText("修改密码");
        pwdModify.setSelected(true);
        pwdModify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseDialogFragment dialog = new BaseDialogFragment();

                TpvCommonItemView tpvCommonItemView = new TpvCommonItemView(getContext());
                tpvCommonItemView.setIconSize(TpvCommonItemView.EIconSize.SMALL);
                tpvCommonItemView.setNextType(TpvCommonItemView.ENextType.SWITCH);
                tpvCommonItemView.setLayoutVisibility(15);
                tpvCommonItemView.setItemOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        tpvCommonItemView.setSwitchStatus(!tpvCommonItemView.getSwitchStatus());
                    }
                });
                dialog.setContentView(tpvCommonItemView);
                dialog.onCancelButtonClick("取消", new BaseDialogFragment.IOnCancelCallback() {
                    @Override
                    public void onCancel() {

                    }
                });
                dialog.onConfirmButtonClick("确定", new BaseDialogFragment.IOnConfirmCallback() {
                    @Override
                    public void onConfirm() {

                    }
                });
                dialog.show(getChildFragmentManager(), BaseDialogFragment.class.getSimpleName());
                dialog.setTitleContent("系统提示");
            }
        });

        titleBarView2RightBtn.addTitleBarRight(pwdSet, pwdModify).setLayoutReadyOk();

        //------------------------------------------------------------------------------------------
        titleBarViewFilter = rootView.findViewById(R.id.layout_titlebar_view_apps_filter);

        TextView btnNormalDisabled = titleBarViewFilter.createTitleRightBtnTextView();
        btnNormalDisabled.setBackground(getResources().getDrawable(R.drawable.selector_bg_rounded_rectangle_large));
        btnNormalDisabled.setEnabled(false);
        btnNormalDisabled.setText("常态禁用");

        TextView btnStartRecord = titleBarViewFilter.createTitleRightBtnTextView();
        btnStartRecord.setBackground(getResources().getDrawable(R.drawable.selector_bg_rounded_rectangle_large));
        btnStartRecord.setText("启动记录");
        btnStartRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.getInstance().showShortToast(v.getContext(), btnStartRecord.getText().toString() + "事件");
            }
        });

        titleBarViewFilter.addTitleBarRight(btnNormalDisabled, btnStartRecord).setLayoutReadyOk();

        //------------------------------------------------------------------------------------------

        initCommonitem(rootView);

        //------------------------------------------------------------------------------------------
        initMenuBar(rootView);

    }

    private void initCommonitem(View rootView) {
        String title = "Widget ";
        //排版内容 00000001:Title
        tpvLayoutItem1 = rootView.findViewById(R.id.layout_item_1);
        tpvLayoutItem1.setTitleContent(title + "1");
        tpvLayoutItem1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: >>>>>>" + tpvLayoutItem1.getTitleContent());
            }
        });

        //排版内容 00000101:Title + Next(arrow)
        tpvLayoutItem5Arrow = rootView.findViewById(R.id.layout_item_5_arrow);
        tpvLayoutItem5Arrow.setTitleContent(title + "5-arrow");
        tpvLayoutItem5Arrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: >>>>>>" + tpvLayoutItem5Arrow.getTitleContent());
            }
        });

        //排版内容 00000101:Title + Next(switch)
        tpvLayoutItem5Switch = rootView.findViewById(R.id.layout_item_5_switch);
        tpvLayoutItem5Switch.setTitleContent(title + "5-switch");
        tpvLayoutItem5Switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = !tpvLayoutItem5Switch.getSwitchStatus();
                tpvLayoutItem5Switch.setSwitchStatus(isChecked);
            }
        });
        tpvLayoutItem5Switch.setItemOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.d(TAG, "onClick: >>>>>>" + tpvLayoutItem5Switch.getTitleContent() + ":" + (isChecked ? "开" : "关"));
            }
        });

        //排版内容 00000101:Title + Next(custom)
        tpvLayoutItem5Custom = rootView.findViewById(R.id.layout_item_5_custom);
        tpvLayoutItem5Custom.setTitleContent(title + "5-custom");
        tpvLayoutItem5Custom.setCustomImageDrawable(getResources().getDrawable(R.mipmap.ic_setup_pressed));
        tpvLayoutItem5Custom.setCustomOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: >>>>>>Next(custom) 事件");
                tpvLayoutItem5Custom.performClick();
            }
        });
        tpvLayoutItem5Custom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: >>>>>>" + tpvLayoutItem5Custom.getTitleContent());
            }
        });


        //排版内容 00010101:Title + Next(arrow) + Mark
        tpvLayoutItem21Arrow = rootView.findViewById(R.id.layout_item_21_arrow);
        tpvLayoutItem21Arrow.setTitleContent(title + "21-arrow");
        tpvLayoutItem21Arrow.setMarkContent("v 1.1.0");
        tpvLayoutItem21Arrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: >>>>>>" + tpvLayoutItem21Arrow.getTitleContent());
            }
        });

        //排版内容 00010111:Title + Icon + Next(arrow) + Mark
        tpvLayoutItem23Arrow = rootView.findViewById(R.id.layout_item_23_arrow);
        tpvLayoutItem23Arrow.setTitleContent(title + "23-arrow");
        tpvLayoutItem23Arrow.setMarkContent("v 1.2.0");
        tpvLayoutItem23Arrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: >>>>>>" + tpvLayoutItem23Arrow.getTitleContent());
            }
        });

        //排版内容 00010111:Title + Icon + Next(switch) + Mark
        tpvLayoutItem23Switch = rootView.findViewById(R.id.layout_item_23_switch);
        tpvLayoutItem23Switch.setTitleContent(title + "23-switch");
        tpvLayoutItem23Switch.setSwitchStatus(true);
        tpvLayoutItem23Switch.setIconImageDrawable(getResources().getDrawable(R.drawable.ic_menu_camera));
        tpvLayoutItem23Switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = !tpvLayoutItem23Switch.getSwitchStatus();
                tpvLayoutItem23Switch.setSwitchStatus(isChecked);
            }
        });
        tpvLayoutItem23Switch.setItemOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.d(TAG, "onClick: >>>>>>" + tpvLayoutItem23Switch.getTitleContent() + ":" + (isChecked ? "开" : "关"));
                tpvLayoutItem23Switch.setLayoutVisibility(isChecked ? 23 : 7);//显示、隐藏 Mark 信息
            }
        });

        //排版内容 00011111:Title + Icon + Next(arrow) + SubTitle + Mark
        tpvLayoutItem31Arrow = rootView.findViewById(R.id.layout_item_31_arrow);
        tpvLayoutItem31Arrow.setTitleContent(title + "31-arrow");
        tpvLayoutItem31Arrow.setIconImageDrawable(getResources().getDrawable(R.drawable.ic_avatar));
        tpvLayoutItem31Arrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: >>>>>>" + tpvLayoutItem31Arrow.getTitleContent());
            }
        });

        //排版内容 00011111:Title + Icon + Next(switch) + SubTitle + Mark
        tpvLayoutItem31Switch = rootView.findViewById(R.id.layout_item_31_switch);
        tpvLayoutItem31Switch.setTitleContent(title + "31-switch");
        tpvLayoutItem31Switch.setIconImageDrawable(getResources().getDrawable(R.drawable.ic_avatar));
        tpvLayoutItem31Switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = !tpvLayoutItem31Switch.getSwitchStatus();
                tpvLayoutItem31Switch.setSwitchStatus(isChecked);
            }
        });
        tpvLayoutItem31Switch.setItemOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.d(TAG, "onClick: >>>>>>" + tpvLayoutItem31Switch.getTitleContent() + ":" + (isChecked ? "开" : "关"));
                tpvLayoutItem31Switch.setIconSize(isChecked ? TpvCommonItemView.EIconSize.DEFAULT : TpvCommonItemView.EIconSize.SMALL);//默认图标和小图标进行切换
            }
        });

        //排版内容 00100101:Title + Next(switch) + Other
        tpvLayoutItem37Switch = rootView.findViewById(R.id.layout_item_37_switch);
        tpvLayoutItem37Switch.setTitleContent(title + "37-switch");
        tpvLayoutItem37Switch.setIconImageDrawable(getResources().getDrawable(R.drawable.ic_avatar));
        tpvLayoutItem37Switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = !tpvLayoutItem37Switch.getSwitchStatus();
                tpvLayoutItem37Switch.setSwitchStatus(isChecked);
            }
        });
        tpvLayoutItem37Switch.setItemOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.d(TAG, "onClick: >>>>>>" + tpvLayoutItem37Switch.getTitleContent() + ":" + (isChecked ? "开" : "关"));
            }
        });

        //排版内容 00111111:Title + Icon + Next(switch) + SubTitle + Mark + Other
        tpvLayoutItem63Switch = rootView.findViewById(R.id.layout_item_63_switch);
        tpvLayoutItem63Switch.setTitleContent(title + "63-switch");
        tpvLayoutItem63Switch.setIconImageDrawable(getResources().getDrawable(R.drawable.ic_avatar));
        tpvLayoutItem63Switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = !tpvLayoutItem63Switch.getSwitchStatus();
                tpvLayoutItem63Switch.setSwitchStatus(isChecked);
            }
        });
        tpvLayoutItem63Switch.setItemOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.d(TAG, "onClick: >>>>>>" + tpvLayoutItem63Switch.getTitleContent() + ":" + (isChecked ? "开" : "关"));
            }
        });
    }

    private int chooseTotal = 0;

    private void initMenuBar(View rootView) {
        View layoutTpvFileMenuBar = rootView.findViewById(R.id.layout_tpv_file_choose);
        final TpvCommonItemView tpvLayoutItemFileChoose = rootView.findViewById(R.id.layout_item_tpv_menubar);
        tpvLayoutFileMenubar = rootView.findViewById(R.id.layout_menubar);
        tpvLayoutItemFileChoose.setItemOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = !tpvLayoutItemFileChoose.getSwitchStatus();
                tpvLayoutItemFileChoose.setSwitchStatus(isChecked);
            }
        });
        tpvLayoutItemFileChoose.setItemOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                layoutTpvFileMenuBar.setVisibility(isChecked ? View.VISIBLE : View.GONE);
                tpvLayoutFileMenubar.setVisibility(layoutTpvFileMenuBar.getVisibility());
            }
        });
        layoutTpvFileMenuBar.setVisibility(tpvLayoutItemFileChoose.getSwitchStatus() ? View.VISIBLE : View.GONE);
        tpvLayoutFileMenubar.setVisibility(layoutTpvFileMenuBar.getVisibility());

        tpvLayoutFileMenubar.setMenuCallback(new TpvFileMenuBarView.IMenuCallback() {
            @Override
            public void onMenuCheckMult() {
                ToastUtils.getInstance().showShortToast(getContext(), "你点击了 多选 按钮");
                chooseTotal = 0;
            }

            @Override
            public void onMenuCheckAll() {
                ToastUtils.getInstance().showShortToast(getContext(), "你点击了 全选 按钮");
                chooseTotal = 3;
            }

            @Override
            public void onMenuCheckCancel() {
                ToastUtils.getInstance().showShortToast(getContext(), "你点击了 取消 按钮");
                chooseTotal = 0;
            }

            @Override
            public void onMenuCheckFile() {
                ToastUtils.getInstance().showShortToast(getContext(), "你点击了 查看 按钮");
            }

            @Override
            public void onMenuInsertIwb() {
                ToastUtils.getInstance().showShortToast(getContext(), "你点击了 插入白板 按钮");
            }

            @Override
            public void onMenuSave() {
                ToastUtils.getInstance().showShortToast(getContext(), "你点击了 保存 按钮");
            }

            @Override
            public void onMenuDel() {
                ToastUtils.getInstance().showShortToast(getContext(), "你点击了 删除 按钮");
                chooseTotal = 0;
            }

            @Override
            public void onMenuExit() {
                ToastUtils.getInstance().showShortToast(getContext(), "你点击了 退出 按钮");
                chooseTotal = 0;
            }
        });

        ((CheckBox) rootView.findViewById(R.id.cb_1)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setMenuBarStatus(isChecked);
            }
        });
        ((CheckBox) rootView.findViewById(R.id.cb_2)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setMenuBarStatus(isChecked);
            }
        });
        ((CheckBox) rootView.findViewById(R.id.cb_3)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setMenuBarStatus(isChecked);
            }
        });
    }

    private void setMenuBarStatus(boolean isChecked) {
        if (isChecked) {
            chooseTotal++;
        } else {
            chooseTotal--;
        }
        if (chooseTotal < 0) {
            chooseTotal = 0;
        }
        if (chooseTotal > 3) {
            chooseTotal = 3;
        }
        Log.d(TAG, "setMenuBarStatus: >>>>>>chooseTotal=" + chooseTotal + ",isChecked=" + isChecked);
        tpvLayoutFileMenubar.setMenuBarStatus(chooseTotal, chooseTotal >= 3);

    }

    /**
     * 选择视图
     *
     * @param view
     * @param isRotate
     */
    private void rotateView(View view, boolean isRotate) {
        if (!isRotate) {
            view.clearAnimation();
            return;
        }
        RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(2000);
        LinearInterpolator lir = new LinearInterpolator();
        rotateAnimation.setInterpolator(lir);
        rotateAnimation.setRepeatMode(Animation.RESTART);
        rotateAnimation.setRepeatCount(Animation.INFINITE);
//        view.setAnimation(rotateAnimation);
        view.startAnimation(rotateAnimation);
    }

//    abstract class SingleClickListener implements View.OnClickListener {
//        private long lastClickTime;
//        private long timeInterval = 1000L;
//        private boolean isValid = true;
//
//        public SingleClickListener() {
//
//        }
//
//        public SingleClickListener(long interval) {
//            this.timeInterval = interval;
//        }
//
//        @Override
//        public void onClick(View v) {
//            if (isValid) {
//                onSingleClick();
//                lastClickTime = System.currentTimeMillis();
//                isValid = false;
//            } else {
//                if (System.currentTimeMillis() - lastClickTime > timeInterval) {
//                    isValid = true;
//                }
//            }
//        }
//
//        protected abstract void onSingleClick();
//    }

}